/**
 * 
 */
package edu.gatech.oad.mule.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.InputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import edu.gatech.oad.mule.view.interfaces.RenderableGameDriver;
import edu.gatech.oad.mule.view.interfaces.RenderablePlayer;

/**
 * Displays information related to the game's ranking phase.
 * 
 * @author Dale Avery
 * 
 */
class PlayerRankingPanel extends JPanel {
	
	/**
	 * Organizes player information into a single panel.
	 * 
	 * @author Dale Avery
	 * 
	 */
	private class PlayerPanel extends JPanel {

		JLabel imageLabel;
		JLabel nameLabel;

		/**
		 * @param place
		 *            The rank this panel displays information for.
		 */
		public PlayerPanel(int place) {
			this.setLayout(new BorderLayout(5, 0));

			// Show the place number for this panel.
			JLabel placeLabel = new JLabel("" + place);
			placeLabel.setHorizontalAlignment(SwingConstants.CENTER);
			placeLabel.setFont(new Font("Verdana", Font.PLAIN, 75));
			this.add(placeLabel, BorderLayout.WEST);

			// Create a label for showing a player's image.
			imageLabel = new JLabel();
			this.add(imageLabel, BorderLayout.EAST);

			// Create a label for showing a player's name.
			nameLabel = new JLabel();
			nameLabel.setFont(new Font("Tahoma", Font.PLAIN, 58));
			this.add(nameLabel, BorderLayout.CENTER);
		}

		/**
		 * Changes the player that the panel displays info for.
		 * @param player The player to display.
		 */
		public void changePlayer(RenderablePlayer player) {
			if (player != null) {
				// Show the player's image.
				imageLabel.setIcon(new ImageIcon(GraphicsLib.buildPlayerImage(player)));
				
				// Display the player's name.
				nameLabel.setText(player.getName());
				nameLabel.setForeground(player.getColor());
				nameLabel.setFont(new Font("Tahoma", Font.PLAIN, 58));
			}
		}
	}

	private static final int MAX_PLAYERS = 4;
	RenderableGameDriver gameDriver;

	PlayerPanel[] rankPanels;
	
	/**
	 * Constructs a new info panel.
	 * 
	 * @param players
	 *            The list of all players for whom information needs to be
	 *            displayed
	 * @param dimension
	 */
	public PlayerRankingPanel(RenderableGameDriver driver, Dimension dimension) {
		
		gameDriver = driver;
		this.setLayout(new BorderLayout());

		// Display a title for the phase.
		JLabel titleLabel = new JLabel("Player Ranking");
		titleLabel.setFont(new Font("Tahoma", Font.PLAIN, 35));
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(titleLabel, BorderLayout.NORTH);

		// Create a panel for containing the ranked player information.
		JPanel rankingPanel = new JPanel();
		add(rankingPanel, BorderLayout.CENTER);
		rankingPanel.setLayout(new GridLayout(MAX_PLAYERS, 1, 0, 0));
		
		// Build the ranking panels for display individual player info
		rankPanels = new PlayerPanel[MAX_PLAYERS];
		for (int i = 0; i < rankPanels.length; i++) {
			rankPanels[i] = new PlayerPanel(i+1);
			rankingPanel.add(rankPanels[i]);
		}

		this.setPreferredSize(dimension);
	}

	/**
	 * Forces the panel to update the ranking information for the players.
	 */
	public void update() {
		RenderablePlayer[] players = gameDriver.getRankedPlayers();
		for (int i = 0; i < players.length && i < rankPanels.length; i++) {
			rankPanels[i].changePlayer(players[players.length - i - 1]);
		}
	}
}
